package com.example.graphicslib.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

import com.example.graphicslib.animations.Animation;
import com.example.graphicslib.animations.BaseRotationAnimation;
import com.example.graphicslib.utils.RotationDirection;

import java.io.IOException;

/**
 * Created by Tatev on 13/4/2014.
 */
public class RotationAnimationExampleView extends View {
    public RotationAnimationExampleView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(image, rotationAnimation.getRotationMatrix(), null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = w;
        height = h;

        init();
    }

    private void init() {

        try {
            image = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(context.getAssets().open("img2.png")),
                    width / 6, width / 6, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        rotationAnimation = new BaseRotationAnimation(5, RotationDirection.COUNTERCLOCKWISE, image.getWidth(), image.getHeight(), 5000);
        rotationAnimation.setCoordinates(width / 3, height / 3);
        //TODO

        animation = new Animation(500, rotationAnimation, this);
        animation.start();
    }

    private Bitmap image;
    private Context context;
    private Animation animation;
    private BaseRotationAnimation rotationAnimation;
    private int width, height;
}
